﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI
{
    public partial class Flags : Form
    {
        public Flags()
        {
            InitializeComponent();
        }

        public Flags(TasksModel.TaskLibrary library)
            : this()
        {
            this.library = library;
            lb.BeginUpdate();
            foreach (TasksModel.TaskFlag tf in library.Flags)
                lb.Items.Add(tf);
            lb.EndUpdate();
        }

        public static void Show(TasksModel.TaskLibrary library)
        {
            using (Flags f = new Flags(library))
            {
                f.ShowDialog(Form.ActiveForm);
            }
        }

        private TasksModel.TaskLibrary library;

        private void lbColor_Click(object sender, EventArgs e)
        {
            Pickpup();
        }

        private void bColor_Click(object sender, EventArgs e)
        {
            Pickpup();
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            string name = tbName.Text;
            TasksModel.TaskFlag t = (TasksModel.TaskFlag)lb.SelectedItem;
            foreach (TasksModel.TaskFlag ta in lb.Items)
                if (ta.Name == name && ta.ID != t.ID)
                {
                    MessageBox.Show(this, "Flag name already exists", "Save");
                    return;
                }
            tbName.Text = string.Empty;
            tbName.Enabled = false;
            bSave.Enabled = false;
            bColor.Enabled = false;
            bRemove.Enabled = false;
            t.SuspendChangedEvent();
            try
            {
                t.Name = name;
                t.RGBColor = lbColor.BackColor.ToArgb() & 0xFFFFFF;
            }
            finally
            {
                t.ResumeChangedEvent();
            }
            lbColor.Enabled = false;
            lbColor.BackColor = Control.DefaultBackColor;
            lb.Items[lb.SelectedIndex] = t;
        }

        private void Pickpup()
        {
            colorDlg.Color = lbColor.BackColor;
            if (colorDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lbColor.BackColor = colorDlg.Color;
            }
        }

        private void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb.SelectedItem == null)
                return;
            TasksModel.TaskFlag tf = (TasksModel.TaskFlag)lb.SelectedItem;
            tbName.Enabled = true;
            bSave.Enabled = true;
            bColor.Enabled = true;
            lbColor.Enabled = true;
            tbName.Text = tf.Name;
            lbColor.BackColor = Color.FromArgb(tf.ToR, tf.ToG, tf.ToB);
            bRemove.Enabled = !library.CheckFlagUsed(tf);
        }

        private void lb_DrawItem(object sender, DrawItemEventArgs e)
        {
            TasksModel.TaskFlag tf = (TasksModel.TaskFlag)lb.Items[e.Index];
            Brush brush = new SolidBrush(Color.FromArgb(tf.ToR, tf.ToG, tf.ToB));
            e.Graphics.FillRectangle(brush, e.Bounds);
            Font drawFont = e.Font;
            if ((e.State & DrawItemState.Selected) > 0)
                drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
            e.Graphics.DrawString(
                ((ListBox)sender).Items[e.Index].ToString(),
                e.Font,
                Brushes.White,
                e.Bounds,
                StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }

        private void bRemove_Click(object sender, EventArgs e)
        {
            TasksModel.TaskFlag t = (TasksModel.TaskFlag)lb.SelectedItem;

            if (library.CheckFlagUsed(t))
            {
                MessageBox.Show(this, "Flag is being used", "Remove");
                return;
            }
            library.Flags.Remove(t);
            lb.Items.RemoveAt(lb.SelectedIndex);
            tbName.Text = string.Empty;
            tbName.Enabled = false;
            bSave.Enabled = false;
            bRemove.Enabled = false;
            bColor.Enabled = false;
            lbColor.Enabled = false;
            lbColor.BackColor = Control.DefaultBackColor;
        }
    }
}